package com.ztc.ZtcClassesForSale.dao;
import com.aqgj.util.BaseDaoImpl;
import com.ztc.ZtcClassesForSale.bean.ZtcClassesForSale;
import org.springframework.stereotype.Repository;


import java.text.SimpleDateFormat;
import java.util.List;
/**
 * <p>
 *
 * </p>
 *
 * @ClassName ZtcClassesForSaleDao
 * @JDK 1.5
 * @author
 * @date
 */
@Repository("ZtcClassesForSaleDao")
public class ZtcClassesForSaleDao extends BaseDaoImpl<ZtcClassesForSale> {
	private static final SimpleDateFormat DATE_TIME_FORMET = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat DATE_FORMET = new SimpleDateFormat("yyyy-MM-dd");
	private int pageSize=10;
	public String save(ZtcClassesForSale ztcClassesForSale) {
		this.getHibernateTemplate().save(ztcClassesForSale);
		return ztcClassesForSale.getId();
	}

	public List<ZtcClassesForSale> getAllZtcClassesForSale() {
		return this.getAll();
	}

	public String delete(String id, String ztcClassesId,String addUserId,String modifyUserId) {
		String sql = "delete from ztc_classes_for_sale where 1=1";
		if (id != null) {
			sql += " and id='" + id + "'";
		}
		if (ztcClassesId != null) {
			sql += " and fk_ztc_classes_id='" + ztcClassesId + "'";
		}
		if (addUserId != null) {
			sql += " and fk_add_user_id='" + addUserId + "'";
		}
		if (modifyUserId != null) {
			sql += " and fk_modify_user_id='" + modifyUserId + "'";
		}
		if (this.currentSession().createNativeQuery(sql).executeUpdate() >= 0) {
			return "1";
		}
		return "0";
	}
	public List<ZtcClassesForSale> searchInfoEquals(ZtcClassesForSale ztcClassesForSale,String beginDate,String endDate) {
		String sql = "select * from ztc_classes_for_sale  where 1=1";
		if (ztcClassesForSale.getId() != null) {
			sql += " and id='" + ztcClassesForSale.getId() + "'";
		}
		if (ztcClassesForSale.getName() != null) {
			sql += " and name='" + ztcClassesForSale.getName() + "'";
		}
		if (ztcClassesForSale.getRemark() != null) {
			sql += " and remark='" + ztcClassesForSale.getRemark() + "'";
		}
		if (ztcClassesForSale.getStatus() != null) {
			sql += " and status='" + ztcClassesForSale.getStatus() + "'";
		}
		if (ztcClassesForSale.getCarType() != null) {
			sql += " and car_type='" + ztcClassesForSale.getCarType() + "'";
		}
		if (ztcClassesForSale.getTicketStatus() != null) {
			sql += " and ticket_status='" + ztcClassesForSale.getTicketStatus() + "'";
		}
		if (ztcClassesForSale.getIsRefund() != null) {
			sql += " and is_refund='" + ztcClassesForSale.getIsRefund() + "'";
		}
		if (ztcClassesForSale.getChildSeatNumber() != null) {
			sql += " and child_seat_number='" + ztcClassesForSale.getChildSeatNumber() + "'";
		}
		if (ztcClassesForSale.getDepartureDate() != null) {
			sql += " and departure_date='" + DATE_FORMET.format(ztcClassesForSale.getDepartureDate()) + "'";
		}
		if (ztcClassesForSale.getAddTime() != null) {
			sql += " and add_time='" + DATE_TIME_FORMET.format(ztcClassesForSale.getAddTime()) + "'";
		}
		if (ztcClassesForSale.getModifyTime() != null) {
			sql += " and modify_time='" + DATE_TIME_FORMET.format(ztcClassesForSale.getModifyTime()) + "'";
		}
		if (ztcClassesForSale.getFkZtcCarId() != null) {
			sql += " and fk_ztc_car_id='" + ztcClassesForSale.getFkZtcCarId() + "'";
		}
		if (ztcClassesForSale.getFkZtcDriverId() != null) {
			sql += " and fk_ztc_driver_id='" + ztcClassesForSale.getFkZtcDriverId() + "'";
		}
		if (ztcClassesForSale.getFkZtcClassesId() != null) {
			sql += " and fk_ztc_classes_id='" + ztcClassesForSale.getFkZtcClassesId() + "'";
		}
		if (ztcClassesForSale.getFkTransportId() != null) {
			sql += " and fk_transport_id='" + ztcClassesForSale.getFkTransportId() + "'";
		}
		if (ztcClassesForSale.getFkAddUserId() != null) {
			sql += " and fk_add_user_id='" + ztcClassesForSale.getFkAddUserId() + "'";
		}
		if (ztcClassesForSale.getFkModifyUserId() != null) {
			sql += " and fk_modify_user_id='" + ztcClassesForSale.getFkModifyUserId() + "'";
		}
		if (beginDate!= null) {
			sql += " and departure_date>='" + beginDate + "'";
		}
		if (endDate != null) {
			sql += " and departure_date<='" + endDate + "'";
		}
		sql+=" order by departure_date";
		List<ZtcClassesForSale> list = this.currentSession().createNativeQuery(sql,ZtcClassesForSale.class).getResultList();
		return list;
	}
	public List<ZtcClassesForSale> searchInfoLikes(ZtcClassesForSale classesForSale, String transportType, String pageNumber) {
		String sql = "select ztc_classes_for_sale.* from ztc_classes_for_sale ,ztc_classes where"
				+" ztc_classes.id=ztc_classes_for_sale.fk_ztc_classes_id"
				+" and ztc_classes_for_sale.ticket_status='0'"
				+" and ztc_classes_for_sale.departure_date>=CURDATE() and ztc_classes_for_sale.departure_date< DATE_ADD(CURDATE(),INTERVAL ztc_classes.pre_sale_days DAY)"
				+" and 1=1";
		if (classesForSale.getName() != null) {
			sql += " and ztc_classes_for_sale.name like '%" + classesForSale.getName() + "%'";
		}
		if (classesForSale.getRemark() != null) {
			sql += " and ztc_classes_for_sale.remark like '%" + classesForSale.getRemark() + "%'";
		}
        if(!"0".equals(transportType)){
        	if (classesForSale.getFkTransportId() != null) {
    			sql += " and ztc_classes_for_sale.fk_transport_id='" + classesForSale.getFkTransportId() + "'";
    		}
		}
        sql+=" and ztc_classes_for_sale.ticket_status='0'";
		sql+=" order by ztc_classes_for_sale.departure_date";
		int startIndex=0;
		int endIndex=10;
		if(pageNumber==null){
			pageNumber="0";
		}else{
			int number=Integer.parseInt(pageNumber);
	    	if(!"0".equals(pageNumber)){
	    		number=number-1;
	    	}
	    	startIndex=(number)*pageSize;
		}
		sql+=" limit "+startIndex+","+endIndex;
		List<ZtcClassesForSale> list = this.currentSession().createNativeQuery(sql,ZtcClassesForSale.class).getResultList();
		return list;
	}

	public String update(ZtcClassesForSale ztcClassesForSale) {
		this.getHibernateTemplate().saveOrUpdate(ztcClassesForSale);
		if (ztcClassesForSale.getId() != null) {
			return "1";
		}
		return "0";
	}
	public int getCount(String classesId,String status) {
		String sql = "select count(*) from ztc_classes_for_sale where "
				+" fk_ztc_classes_id='"+classesId+"'";
		if(status!=null){
			sql+=" and status='"+status+"'";
		}
		@SuppressWarnings("unchecked")
		List<Object> li= this.getHibernateTemplate().getSessionFactory().getCurrentSession().createNativeQuery(sql).getResultList();
		return new Integer(String.valueOf(li.get(0)));
	}
	public int getCount(String name,String transportType,String transportId) {
		String sql = "select count(ztc_classes_for_sale.id) from ztc_classes_for_sale ,ztc_classes where"
				+" ztc_classes.id=ztc_classes_for_sale.fk_ztc_classes_id"
				+" and ztc_classes_for_sale.ticket_status='0'"
				+" and ztc_classes_for_sale.departure_date>=CURDATE() and ztc_classes_for_sale.departure_date< DATE_ADD(CURDATE(),INTERVAL ztc_classes.pre_sale_days DAY)"
				+" and 1=1";
		if (name != null) {
			sql += " and ztc_classes_for_sale.name like '%" + name + "%'";
		}
		if(!"0".equals(transportType)){
			if(transportId!=null){
				sql += " and ztc_classes_for_sale.fk_transport_id='" + transportId + "'";
			}
		}
		sql+=" order by ztc_classes_for_sale.departure_date";
		@SuppressWarnings("unchecked")
		List<Object> li= this.getHibernateTemplate().getSessionFactory().getCurrentSession().createNativeQuery(sql).getResultList();
		return new Integer(String.valueOf(li.get(0)));
	}
	public List<ZtcClassesForSale> searchInfoWithClassesId(String classesId) {
		String sql = "select * from ztc_classes_for_sale where"
				+" ztc_classes_for_sale.ticket_status='0'";
			/*	+" and ztc_classes_for_sale.departure_date>=CURDATE()";*/
		if (classesId != null) {
			sql += " and ztc_classes_for_sale.fk_ztc_classes_id = '" + classesId + "'";
		}
		List<ZtcClassesForSale> list = this.currentSession().createNativeQuery(sql,ZtcClassesForSale.class).getResultList();
		return list;
	}
}